//
//  NSMutableString+Safe.m
//  DebugDemo
//
//  Created by XFB on 2018/11/9.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "NSMutableString+Safe.h"
#import "NSObject+Swizzling.h"

///// 不可变字符串
//static NSString *KMutableStringClass = @"__NSCFConstantString";

@implementation NSMutableString (Safe)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [objc_getClass("__NSCFConstantString") swizzleSelector:@selector(replaceCharactersInRange:withString:)
                                  withSwizzledSelector:@selector(zc_safeReplaceCharactersInRange:withString:)];
        [objc_getClass("__NSCFConstantString") swizzleSelector:@selector(insertString:atIndex:)
                                  withSwizzledSelector:@selector(zc_safeInsertString:atIndex:)];
        [objc_getClass("__NSCFConstantString") swizzleSelector:@selector(deleteCharactersInRange:)
                                  withSwizzledSelector:@selector(zc_safeDeleteCharactersInRange:)];
        [objc_getClass("__NSCFConstantString") swizzleSelector:@selector(appendString:)
                                  withSwizzledSelector:@selector(zc_safeAppendString:)];
        [objc_getClass("__NSCFConstantString") swizzleSelector:@selector(setString:)
                                  withSwizzledSelector:@selector(zc_safeSetString:)];
        
        
        /// 新增
        
        
        
    });
}

- (void)zc_safeReplaceCharactersInRange:(NSRange)range withString:(NSString *)aString
{
    if(range.location + range.length > self.length || !aString) return;
    [self zc_safeReplaceCharactersInRange:range withString:aString];
}

- (void)zc_safeInsertString:(NSString *)aString atIndex:(NSUInteger)location
{
    if (!aString || location > self.length) return;
    [self zc_safeInsertString:aString atIndex:location];
}

- (void)zc_safeDeleteCharactersInRange:(NSRange)range
{
    if(range.location + range.length > self.length) return;
    [self zc_safeDeleteCharactersInRange:range];
}

- (void)zc_safeAppendString:(NSString *)aString
{
    if(!aString) return;
    [self zc_safeAppendString:aString];
}

- (void)zc_safeSetString:(NSString *)aString
{
    if(!aString) return;
    [self zc_safeSetString:aString];
}

@end
